package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.bo.member.MemberBO;
import com.ymk.bo.member.MemberFinanceLogBO;
import com.ymk.core.code.cmd.MemberCmdCode;
import com.ymk.service.IMemberFinanceLogService;
import com.ymk.service.impl.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Objects;

@Service
public class MemberFinanceCacheCmd extends ACmd {

    @Override
    public CmdSign executeServer(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        MemberFinanceLogBO reqData = super.getData(sign, MemberFinanceLogBO.class);

        // 合计所有积分日志
        BigDecimal sumAmount = this.memberFinanceLogService.sumMemberFinanceAmount(reqData);

        MemberBO memberBO = new MemberBO();
        memberBO.setId(reqData.getMemberId());
        memberBO.setPoints(sumAmount);
        this.memberService.update(memberBO);

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        MemberFinanceLogBO reqData = super.getData(sign, MemberFinanceLogBO.class);
        if (Objects.isNull(reqData.getMemberId())){
            throw new MallException("会员id为空");
        }
        return null;
    }

    @Override
    public String getCmdCode() {
        return MemberCmdCode.MEMBER_FINANCE_CACHE;
    }

    @Autowired
    private IMemberFinanceLogService memberFinanceLogService;
    @Autowired
    private MemberService memberService;
}